package com.xpsoft.oa.service.produce.impl;


import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.reflect.TypeToken;
import com.xpsoft.core.service.impl.BaseServiceImpl;
import com.xpsoft.core.util.BeanUtil;
import com.xpsoft.core.util.JsonUtil;
import com.xpsoft.oa.dao.produce.MaterialPurchaseAdjustDao;
import com.xpsoft.oa.dao.produce.MaterialPurchaseAdjustInfoDao;
import com.xpsoft.oa.dao.produce.MaterialPurchaseDao;
import com.xpsoft.oa.dao.produce.MaterialPurchaseInfoDao;
import com.xpsoft.oa.model.produce.MaterialPurchaseAdjust;
import com.xpsoft.oa.model.produce.MaterialPurchaseAdjustInfo;
import com.xpsoft.oa.model.produce.MaterialPurchaseInfo;
import com.xpsoft.oa.service.produce.MaterialPurchaseAdjustService;

public class MaterialPurchaseAdjustServiceImpl extends BaseServiceImpl<MaterialPurchaseAdjust> implements MaterialPurchaseAdjustService{
	private MaterialPurchaseAdjustDao dao;
	@Autowired
	private MaterialPurchaseAdjustInfoDao materialPurchaseAdjustInfoDao;
	@Autowired
	private MaterialPurchaseInfoDao materialPurchaseInfoDao;
	@Autowired
	private MaterialPurchaseDao materialPurchaseDao;
	public MaterialPurchaseAdjustServiceImpl(MaterialPurchaseAdjustDao dao) {
		super(dao);
		this.dao=dao;
	}

	@Override
	public void saveMaterialPurchaseAdjust(
			MaterialPurchaseAdjust materialPurchaseAdjust, String dataList) {
		// TODO Auto-generated method stub
		dao.save(materialPurchaseAdjust);
//		Boolean isNew = true;//新增标志
//		if(materialPurchaseAdjust.getId()!=null){
//			isNew = false;
//		}
		if(StringUtils.isNotBlank(dataList)){
			List<MaterialPurchaseAdjustInfo> materialPurchaseAdjustInfoList = JsonUtil.fromJsonTypesWithDate(dataList,
					new TypeToken<List<MaterialPurchaseAdjustInfo>>() {
					}.getType());
//			if(!isNew){//修改
//				List<Long> idList = new ArrayList<Long>();
//				for(MaterialPurchaseAdjustInfo materialPurchaseAdjustInfo:materialPurchaseAdjustInfoList){
//					if(materialPurchaseAdjustInfo.getId()!=null){//修改的记录
//						idList.add(materialPurchaseAdjustInfo.getId());
//					}
//				}
//				if(idList.size()>0){//先进行删除
//					materialPurchaseAdjustInfoDao.deleteMaterialPurchaseAdjustInfos(materialPurchaseAdjust.getId(), idList);
//				}
//			}
			for(MaterialPurchaseAdjustInfo materialPurchaseAdjustInfo:materialPurchaseAdjustInfoList){
				Long purchaseInfoId = materialPurchaseAdjustInfo.getId();
				materialPurchaseAdjustInfo.setId(null);
				materialPurchaseAdjustInfo.setMaterialPurchaseAdjust(materialPurchaseAdjust);
				materialPurchaseAdjustInfo.setCreateUser(materialPurchaseAdjust.getCreateUser());
				materialPurchaseAdjustInfo.setCreateTime(materialPurchaseAdjust.getCreateTime());
				materialPurchaseAdjustInfoDao.save(materialPurchaseAdjustInfo);
				
				//处理 物资采购计划内容自动根据调正内容进行变更
				if(purchaseInfoId!=null){
					MaterialPurchaseInfo materialPurchaseInfo = materialPurchaseInfoDao.get(purchaseInfoId);
					if(StringUtils.isNotBlank(materialPurchaseAdjustInfo.getAddOrMinus())){
						if("增加".equals(materialPurchaseAdjustInfo.getAddOrMinus())){
							materialPurchaseInfo.setNum(materialPurchaseInfo.getNum()+materialPurchaseAdjustInfo.getChangeNum());
						}else if("减少".equals(materialPurchaseAdjustInfo.getAddOrMinus())){
							materialPurchaseInfo.setNum(materialPurchaseInfo.getNum()-materialPurchaseAdjustInfo.getChangeNum());
						}
					}
					materialPurchaseInfoDao.save(materialPurchaseInfo);
				}else{
					MaterialPurchaseInfo materialPurchaseInfo = new MaterialPurchaseInfo();
					try {
						BeanUtil.copyNotNullProperties(materialPurchaseInfo, materialPurchaseAdjustInfo);
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					materialPurchaseInfo.setId(null);
					materialPurchaseInfo.setPurchasePlan(materialPurchaseAdjust.getMaterialPurchase());
					materialPurchaseInfo.setCreateTime(materialPurchaseAdjust.getCreateTime());
					materialPurchaseInfo.setCreateUser(materialPurchaseAdjust.getCreateUser());
					materialPurchaseInfoDao.save(materialPurchaseInfo);
				}
			} 
		}
	}

}